package app.web.cartoon.report.year.tab;

import java.util.HashMap;
import java.util.Map;

import app.web.spe.CartoonSPE;
import cn.lily.com.component.widget.TElement;
import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.exception.YaconException;
import cn.lily.com.lang.BusinessDate;
import cn.lily.com.report.base.data.mark.EMarkLine;
import cn.lily.com.report.base.data.series.ESeries;
import cn.lily.com.report.base.entity.EToolBox;
import cn.lily.com.report.base.entity.EToolTip;
import cn.lily.com.report.base.entity.axis.type.EAxisCategory;
import cn.lily.com.report.base.entity.toolbox.EMagicType;
import cn.lily.com.report.com.bar.EBarCharts;
import cn.lily.web.page.BusinessPage;
import cn.lily.web.widget.page.TPanel;
/**
 * 动漫季度统计
 * @author JiaJ
 * @date 2017年12月7日
 *
 */
public class FanDetailReport extends BusinessPage {

	@Override
	public void page_onLoad() throws YaconException {
		
		TPanel cartoonFanPanel = Panel("cartoonDetailPanel");
		cartoonFanPanel.creatToolbar().setBarText("【"+BusinessDate.getToday()+"】动漫总数统计");
		
		//无关闭按钮
		cartoonFanPanel.setClose(false);
		cartoonFanPanel.createPanelCell(this.instCharts());
		
		getPage().addElement(cartoonFanPanel);
		
	}

	private TElement instCharts() {
		
		//柱形图对象
		EBarCharts barCharts = new EBarCharts();
		
		//悬浮提示对象 - 线状悬浮
		barCharts.createToolTip(EToolTip.TRIGGER_AXIS);
		//图例
		barCharts.createLegend("春季番,夏季番,秋季番,冬季番");
		
		//工具箱  -------------------------------------
		EToolBox toolBox = barCharts.createToolBox();
		//辅助箱工具
		toolBox.createMark();
		//数据视图工具,设置只读属性false
		toolBox.createDataView().setReadOnly(false);
		//图表转换工具
		toolBox.createMagicType()
			   .addType(EMagicType.EMAGIC_BAR)      //柱形图
			   .addType(EMagicType.EMAGIC_LINE);    //折线图
		//刷新工具
		toolBox.createRestore();
		//图片保存工具
		toolBox.createSaveImage();
		
		//创建拖拽对象
		barCharts.createCalculable();
		
		//创建X轴对象 - 创建对象X轴
		EAxisCategory xCategory = barCharts.createXAxis().createAxisCategory();
		
		int beginYear = CartoonSPE.MAX_YEAR;
		int endYear = CartoonSPE.MIN_YEAR;
		
		StringBuffer years = new StringBuffer();
		for(;beginYear>=endYear;beginYear--){		
			years.append(","+beginYear+"年");		
		}
		
		//添加X轴数据集
		xCategory.addEasyDatas(years.toString().substring(1));
		
		//创建Y轴对象 - 创建数据Y轴
		barCharts.createYAxis().createAxisValue();
		
		//数据集对象
		ESeries series = barCharts.createSeries();
		
		//填入数据
		createData(series);
		
		return barCharts;
	}
	
	private void createData(ESeries series) {
		
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		
		String sql = "select SUM(CAN_DETAIL_NUM) NUM,CAN_FANS from ${schema}.LEO_CAN_REPORT_FANS group by CAN_FANS ";
		DataTable data = DBFactory.getActionDB().executeQueryTable(sql);
		Map<String,Integer> fans = new HashMap<String, Integer>();
		while(data.next()){
			fans.put(data.getString("CAN_FANS"), data.getInt("NUM"));
		}
		
		int beginYear = CartoonSPE.MAX_YEAR;
		int endYear = CartoonSPE.MIN_YEAR;
		
		StringBuffer spring = new StringBuffer();
		StringBuffer summer = new StringBuffer();
		StringBuffer autumn = new StringBuffer();
		StringBuffer winter = new StringBuffer();
		for(;beginYear>=endYear;beginYear--){	
			
			if(fans.containsKey(beginYear+"01")){
				spring.append(","+fans.get(beginYear+"01"));
			}else{
				spring.append(",0");
			}
			
			if(fans.containsKey(beginYear+"04")){
				summer.append(","+fans.get(beginYear+"04"));
			}else{
				summer.append(",0");
			}
			
			if(fans.containsKey(beginYear+"07")){
				autumn.append(","+fans.get(beginYear+"07"));
			}else{
				autumn.append(",0");
			}
			
			if(fans.containsKey(beginYear+"10")){
				winter.append(","+fans.get(beginYear+"10"));
			}else{
				winter.append(",0");
			}
			
		}
		
		//第1条记录
		ESeries series1 = series.createESeries();
		series1.setName("春季番");
		series1.setType(ESeries.SERIES_TYPE_BAR);
		series1.createData().addEasyDatas(spring.toString().substring(1));

		//标记线
		EMarkLine markLine1 = series1.createMarkLine();
		markLine1.createData().setName("平均值").setType("average");
		
		//第2条记录
		ESeries series2 = series.createESeries();
		series2.setName("夏季番");
		series2.setType(ESeries.SERIES_TYPE_BAR);
		series2.createData().addEasyDatas(summer.toString().substring(1));

		//标记线
		EMarkLine markLine2 = series2.createMarkLine();
		markLine2.createData().setName("平均值").setType("average");
		
		//第3条记录
		ESeries series3 = series.createESeries();
		series3.setName("秋季番");
		series3.setType(ESeries.SERIES_TYPE_BAR);
		series3.createData().addEasyDatas(autumn.toString().substring(1));

		//标记线
		EMarkLine markLine3 = series3.createMarkLine();
		markLine3.createData().setName("平均值").setType("average");
		
		//第4条记录
		ESeries series4 = series.createESeries();
		series4.setName("冬季番");
		series4.setType(ESeries.SERIES_TYPE_BAR);
		series4.createData().addEasyDatas(winter.toString().substring(1));

		//标记线
		EMarkLine markLine4 = series4.createMarkLine();
		markLine4.createData().setName("平均值").setType("average");

	}

}
